package com.tms.app.tms.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.tms.app.tms.R;
import com.tms.app.tms.bean.GoodsInfo;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class InputDialog {

    public  static final int btnOne=1;
    public  static final int btnTwo=2;
    public  static final int btnThree=3;
    private  View.OnClickListener myLinser=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLoadingDialog!=null){
                close();
            }
        }
    };
    Dialog mLoadingDialog;


    public static InputDialog getInstance(Context context, View.OnClickListener listener, GoodsInfo goodsInfo,
                                          String title, String textTile,int type) {
        return  new InputDialog(context,listener,goodsInfo,
                title,textTile,type);
    }

    private InputDialog(Context context, View.OnClickListener listener, final GoodsInfo goodsInfo,
                        String title, String textTile, final int type) {
        if (mLoadingDialog!=null&&mLoadingDialog.isShowing())
            return;
        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate( R.layout.layout_at_intput, null);
        // 获取整个布局
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
        TextView textView1_1=view.findViewById(R.id.text_1_1);
        TextView textView2_1=view.findViewById(R.id.text_2_1);
        EditText textView2_2=view.findViewById(R.id.text_2_2);
        textView1_1.setText(title);
        textView2_1.setText(textTile);
        if (type>2){
            textView2_2.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        textView2_2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                switch (type){
                    case 1:
                        goodsInfo.setBillNo(s.toString());
                        break;
                    case 2:
                        goodsInfo.setGoodName(s.toString());
                        break;
                    case 3:
                        if (s.length()>0)
                            try {
                                goodsInfo.setPiece(Integer.parseInt(s.toString()));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        break;
                    case 4:
                        if (s.length()>0)
                            try {
                                goodsInfo.setWeight(Integer.parseInt(s.toString()));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        break;
                    case 5:
                        if (s.length()>0)
                            try {
                                goodsInfo.setVolume(Integer.parseInt(s.toString()));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        break;

                }
            }

        });

        TextView button1=view.findViewById(R.id.dialog_photo_button_1);
        TextView button2=view.findViewById(R.id.dialog_photo_button_2);

        button1.setOnClickListener(myLinser);
        button2.setOnClickListener(myLinser);
        if (listener!=null){
//            button1.setOnClickListener(listener);
            button2.setOnClickListener(listener);
        }


        // 创建自定义样式的Dialog
        //   mLoadingDialog = new Dialog(context, R.style.loading_dialog);

        mLoadingDialog = new Dialog(context);
        // 设置返回键无效
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(true);
        mLoadingDialog.setContentView(view);
    }



    public void show(){
        if (mLoadingDialog!=null&&mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public boolean isShowing(){
        if (mLoadingDialog==null)
            return false;
        return mLoadingDialog.isShowing();
    }

    public void close(){
        if (mLoadingDialog!=null) {
            if (isShowing())
                mLoadingDialog.dismiss();
            mLoadingDialog=null;
        }
    }

}
